<?php

namespace App\Http\Controllers\admin;

use App\Exports\ArticleExport;
use Maatwebsite\Excel\Facades\Excel;
use App\Http\Controllers\Controller;
use App\Models\Article;
use Illuminate\Http\Request;

class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        if($request->header('X-Requested-With') == 'XMLHttpRequest'){

            $draw = $request->get('draw');
            //分页开始位置
            $strat = $request->get('start');
            //分页长度
            $length = $request->get('length');
            //搜索内容
            $search = $request->get('search')['value'];

            if(empty($search)){
                $article = Article::when($search,function ($query) use ($search){
                    $query->where('title','like','%'.$search.'%');
                })
                    ->orderBy('id','desc')
                    ->offset($strat)
                    ->limit($length);
                $data = $article->get();
                //文章总数
                $total = $article->count();

                return [
                    'draw'=>$draw,
                    'recordsTotal'=>$total,
                    'recordsFiltered'=>$total,
                    'data'=>$data,
                ];
            }else{

                $article = Article::
                    orderBy('id','desc')
                    ->offset($strat)
                    ->limit($length);

                $data = $article->get();
                //文章总数
                $total = $article->count();

                return [
                    'draw'=>$draw,
                    'recordsTotal'=>$total,
                    'recordsFiltered'=>$total,
                    'data'=>$data
                ];
            }
        }

        return view('admin.article.index');
    }

    /**
     * 文章添加页
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.article.create');
    }

    /**
     * 执行文章添加
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        $params = $request->except('_token');

        //把地址转化为经纬度
        $geo =sprintf(config('geo.url'),config('geo.key'),$params['address']);

        $encode=file_get_contents($geo);

        $decode=json_decode($encode,true);

        $location = $decode['geocodes'][0]['location'];

        $locationArr = explode(',',$location);

        list($params['longitute'],$params['latitute']) = $locationArr;
        unset($params['address']);

        $res = Article::create($params);

        if($res){
            return  redirect(route('admin.article.index'));
        }else{
            return  redirect(route('admin.article.create'))->withErrors('文章添加失败');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    /**
     *文件异步上传到本地
     * @param Request $request
     */
    public function localFile(Request $request)
    {
        $params = $request->file('file')->store('','article');

        $pid = '/uploads/article/'.$params;

        return $pid;
    }

    /**
     * 文章内容导出
     * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
     */
    public function export()
    {
        return Excel::download(new ArticleExport(), 'article.xlsx');
    }
}
